#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from sale import *
from time import strftime
import cgi
import logging

form = cgi.FieldStorage()
msg=""
err=0

print u"Content-type: text/plain"
print

# sale_id passer lors de la transaction ***
if form.has_key(u"item_number"): 
   sale_id = form[u"item_number"].value  
else:
   msg += u"<br/>*** Champ item_number vide ! ***"
   err=1

# reference Facture passer lors de la transaction ***
if form.has_key(u"item_name"): 
   item_name = form[u"item_name"].value  
else:
   msg += u"<br/>*** Champ item_name vide ! ***"
   err=1

# verified ou unverified ***
if form.has_key(u"payment_status"): 
   payment_status = form[u"payment_status"].value  
else:
   msg += u"<br/>*** Champ payment_status vide ! ***"
   err=1

# montant de la transaction ***
if form.has_key(u"mc_gross"): 
   mc_gross = form[u"mc_gross"].value  
else:
   msg += u"<br/>*** Champ mc_gross vide ! ***"
   err=1

# devise du payment ***
if form.has_key(u"mc_currency"): 
   mc_currency = form[u"mc_currency"].value  
else:
   msg += u"<br/>*** Champ mc_currency vide ! ***"
   err=1

# identifiant paypal de la transaction ***
if form.has_key(u"txn_id"): 
   txn_id = form[u"txn_id"].value  
else:
   msg += u"<br/>*** Champ txn_id vide ! ***"
   err=1


## NOTIFICATION PAYMENT  PAYPAL.........
if err == 0:
  sale=Sale()
  sale_pay=u"PayPal ID: %s\nMontant transaction: %s\nDevise: %s\nPaiement status: %s\nReferences: %s" % ( txn_id, mc_gross, mc_currency, payment_status, item_name  )
  sale_date_pay=strftime("%Y-%m-%d %H:%M:%S")
  sale.updatePay( sale_id, sale_pay, sale_date_pay)
else:
  logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)
  logging.debug(u'notifyPaypal.py error : %s' % msg)
